package com.cloud.school.app.service.impl;

import com.cloud.school.app.entity.TbCollege;
import com.cloud.school.app.mapstruct.ICollegeConvertMapper;
import com.cloud.school.app.model.CollegeModel;
import com.cloud.school.app.model.ImageModel;
import com.cloud.school.app.repository.TbCollegeMapper;
import com.cloud.school.app.service.CollegeService;
import com.cloud.school.app.service.ImageService;
import com.cloud.school.app.service.MajorService;
import com.cloud.school.app.util.IdWorker;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName CollegeServiceImpl
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/14 0:52
 * @Version 1.0
 */
@Service
public class CollegeServiceImpl implements CollegeService {

    @Autowired
    private TbCollegeMapper tbCollegeMapper;
    @Autowired
    private ICollegeConvertMapper collegeConvertMapper;
    @Autowired
    private IdWorker collegeIdWorker;
    @Autowired
    private MajorService majorService;
    @Autowired
    private ImageService imageService;

    @Override
    @Transactional
    public boolean save(CollegeModel collegeModel) {
        if(collegeModel.getImage()==null){
            collegeModel.setImage(242274432331980800l);
        }
        TbCollege college = collegeConvertMapper.modelToEntity(collegeModel);
        college.setId(collegeIdWorker.nextId());
        college.setCreateTime(System.currentTimeMillis());
        return tbCollegeMapper.insertSelective(college) > 0;
    }

    @Override
    public List<CollegeModel> selectAll() {
        return collegeConvertMapper.entityToModel(tbCollegeMapper.selectAll());
    }

    @Override
    public List<CollegeModel> selectDetailAll() {
        List<CollegeModel> collegeModels = selectAll();
        appendImage(collegeModels);
        return collegeModels;
    }

    private void appendImage(List<CollegeModel> collegeModels) {
        List<Long> imageIds = collegeModels.stream().map(c -> c.getImage()).filter(id -> id != null).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(imageIds)) {
            Map<Long, ImageModel> imageModelMap = imageService.seleteByIds(imageIds).stream().collect(Collectors.toMap(ImageModel::getId, i -> i));
            collegeModels.forEach(c -> c.setImageModel(imageModelMap.get(c.getImage())));
        }
    }

    @Override
    public List<CollegeModel> queryByIds(List<Long> collegeIds) {
        if (CollectionUtils.isEmpty(collegeIds)) {
            return Lists.newArrayList();
        }
        String collegeIdArrayString = String.join(",", collegeIds.stream().distinct().map(id -> id.toString()).collect(Collectors.toList()));
        return collegeConvertMapper.entityToModel(tbCollegeMapper.selectByIds(collegeIdArrayString));
    }

    @Override
    @Transactional
    public boolean delete(Long collegeId) {
        majorService.deleteByCollegeId(collegeId);
        return tbCollegeMapper.deleteByPrimaryKey(collegeId) > 0;
    }
}
